package com.sgb.kjwl.viewmodel.packers;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.QuotedPriceSearchEntity;
import com.sgb.kjwl.view.ui.fragment.packers.QuotePriceSearchFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*@author liwei
*@date 2021/2/18
*description:
*/
public class FragQuotePriceSearchModel extends BaseViewModel {

    private QuotePriceSearchFragment fragment;

    private PickSelectEntity all;

    private int page = 1;
    private int limit = 10;

    public FragQuotePriceSearchModel(QuotePriceSearchFragment fragment) {
        this.fragment = fragment;
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getQuotedPriceSearch(String keyword, String categoryNo, String workType, String provinceNo, String cityNo, String areaNo, boolean b) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String,Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        map.put("limit", limit);
        map.put("page", page);
        map.put("keyword", keyword);
        if (!TextUtils.isEmpty(categoryNo)) {
            map.put("categoryNo", categoryNo);
        }
        if (!TextUtils.isEmpty(workType)) {
            map.put("workType", workType);
        }
        if (!TextUtils.isEmpty(provinceNo)) {
            map.put("provinceNo", provinceNo);
        }
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
        }
        if (!TextUtils.isEmpty(areaNo)) {
            List<String> stringList = new ArrayList<>();
            stringList.add(areaNo);
            map.put("areaNo", stringList);
        }
        APIManager.getInstance().getPackerAPI().getQuotedPriceSearch(map).enqueue(new Callback<BaseEntity<List<QuotedPriceSearchEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<QuotedPriceSearchEntity>>> call, Response<BaseEntity<List<QuotedPriceSearchEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        fragment.showQuotePriceList(response.body().getData(), b, response.body().getData().size() < limit);
                    }else {
                        fragment.showEmptyView();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<QuotedPriceSearchEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // -------------------------------- 工程-工程分类 --------------------------------

    public void getCategoryType() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getCategoryListTree(0).
                enqueue(new Callback<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Response<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }

                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            fragment.showCategoryPop(response.body().getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    // ------------------------------------------工种名称 -----------------------------------------------

    public void getWorkType() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getTeamPriceAPI().getWorkType().
                enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {

                        DialogHelper.dismissProgressDialog();
                        if (response.body() != null && response.body().getState() != null && response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            List<PickSelectEntity> entityList = response.body().getData();
                            entityList.add(0, all);
                            fragment.showWorkTypePopWindow(entityList);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    // -------------------------------- 省市区 --------------------------------

    public void getCityList() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getCityList().enqueue(new Callback<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Response<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        for (int i = 0; i < response.body().getData().size(); i++) {
                            fragment.showCityPop(response.body().getData());
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
